﻿namespace RialternativeSample.Client.Northwind
{
    using System;
    using System.Collections.ObjectModel;
    using Microsoft.Windows.Data.DomainServices;
    using RialternativeSample.Client.Northwind.Interfaces;
    using RialternativeSample.Server.NorthwindService;

    public class CustomersDataService : ICustomersDataService
    {
        private NorthwindContext context;

        public CustomersDataService(NorthwindContext context)
        {
            this.context = context;
        }

        public bool CanEditCustomers
        {
            get
            {
                return this.context.CanUpdateCustomers;
            }
        }

        public bool CanDeleteCustomers
        {
            get
            {
                return this.context.CanDeleteCustomers;
            }
        }

        public void GetCustomersAsync(Action<ObservableCollection<Customers>> callback)
        {
            this.context.Load(
                this.context.GetCustomersQuery(),
                lo => callback(new EntityList<Customers>(this.context.Customers, lo.Entities)),
                null);
        }

        public void DeleteCustomer(Customers customer)
        {
            this.context.Customers.Remove(customer);
        }

        public void PersistChangesAsync(Action callback)
        {
            if (callback == null)
            {
                this.context.SubmitChanges();
            }
            else
            {
                this.context.SubmitChanges(so => callback(), null);
            }
        }
    }
}
